package com.mycompany.project.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Style.Position;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class EasyMemoryGUI implements EntryPoint {
	GameMechanics game = new GameMechanics();
	FlowPanel verticalPanel = new FlowPanel();
	HorizontalPanel horizontalPanel = new HorizontalPanel();
	Image image = new Image("images/pickme.png");
	private static final int PLAYERONE = 9;
	private static final int PLAYERTWO = 10;
	private static final String PLAYERRESTART = "MEMORY";
	private static final int DELAY = 1000;
	Label lblPlayerName = new Label(PLAYERRESTART);
	FlexTable flexTable = new FlexTable();
	Button btnreset = new Button("Next Turn?");
	boolean secondPick = false;
	boolean gameRunning = true;
	boolean match = false;
	boolean gameOver=false;
	
	Timer t = new Timer(){
		@Override
		public void run() {
			btnreset.setVisible(true);				
		}};
	
			
	Image image1 = new Image((String) null);
	Image image2 = new Image((String) null);
	Image image3 = new Image((String) null);
	Image image4 = new Image((String) null);
	Image image5 = new Image((String) null);
	Image image6 = new Image((String) null);
	Image image7 = new Image((String) null);
	Image image8 = new Image((String) null);
	Image image9 = new Image((String) null);
	Image image10 = new Image((String) null);
	Image image11 = new Image((String) null);
	Image image12 = new Image((String) null);
	Image image13 = new Image((String) null);
	Image image14 = new Image((String) null);
	Image image15 = new Image((String) null);
	Image image16 = new Image((String) null);
	
	
	public void onModuleLoad() {
		RootPanel rootPanel = RootPanel.get("wrapper");
		rootPanel.getElement().getStyle().setPosition(Position.RELATIVE);
				
		rootPanel.add(verticalPanel, 22, 10);
		verticalPanel.setSize("900px", "1200px");
	
		verticalPanel.add(horizontalPanel);	
		horizontalPanel.setSpacing(10);
		horizontalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel.add(image);
		image.setSize("98px", "94px");
		horizontalPanel.add(lblPlayerName);
		lblPlayerName.setStyleName("playerName");
		horizontalPanel.add(btnreset);
		btnreset.setSize("135px", "52px");
		btnreset.setVisible(false);
		btnreset.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				if(gameOver){
					game.resetGame();
					gameOver=false;
					
				}else{
					game.swapPlayer();
				}
				showName();
				secondPick=false;
				gameRunning=true;
				match=false;
				resetBoard();
				game.setPick(0);
				btnreset.setVisible(false);				
			}});
		
		verticalPanel.add(flexTable);
		flexTable.setStyleName("memoryTable");
				image1.addClickHandler(new ClickHandler(){
					@Override
					public void onClick(ClickEvent event) {
						showPic(1);					
					}});
		
				flexTable.setWidget(1,0,image1);
				image1.setSize("110px", "110px");
				image1.setUrl("images/pickme.png");
				image2.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						showPic(2);
					}
				});
				flexTable.setWidget(1,1,image2);
				image2.setSize("110px", "110px");
				image2.setUrl("images/pickme.png");
				flexTable.setWidget(1,2,image3);
				image3.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						showPic(3);
					}
				});
				image3.setSize("110px", "110px");
				image3.setUrl("images/pickme.png");
				flexTable.setWidget(1,3,image4);
				image4.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						showPic(4);
					}
				});
				image4.setSize("110px", "110px");
				image4.setUrl("images/pickme.png");
				flexTable.setWidget(2,0,image5);
				image5.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						showPic(5);
					}
				});
				image5.setSize("110px", "110px");
				image5.setUrl("images/pickme.png");
				flexTable.setWidget(2,1,image6);
				image6.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						showPic(6);
					}
				});
				image6.setSize("110px", "110px");
				image6.setUrl("images/pickme.png");
				
				flexTable.setWidget(2,2,image7);
				image7.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						showPic(7);
					}
				});
				image7.setSize("110px", "110px");
				image7.setUrl("images/pickme.png");
				flexTable.setWidget(2,3,image8);
				image8.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						showPic(8);
					}
				});
				image8.setSize("110px", "110px");
				image8.setUrl("images/pickme.png");
				flexTable.setWidget(3,0,image9);
				image9.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						showPic(9);
					}
				});
				image9.setSize("110px", "110px");
				image9.setUrl("images/pickme.png");
				flexTable.setWidget(3,1,image10);
				image10.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						showPic(10);
					}
				});
				image10.setSize("110px", "110px");
				image10.setUrl("images/pickme.png");
				flexTable.setWidget(3,2,image11);
				image11.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						showPic(11);
					}
				});
				image11.setSize("110px", "110px");
				image11.setUrl("images/pickme.png");
				flexTable.setWidget(3,3,image12);
				image12.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						showPic(12);
					}
				});
				image12.setSize("110px", "110px");
				image12.setUrl("images/pickme.png");
				
				flexTable.setWidget(4,0,image13);
				image13.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						showPic(13);
					}
				});
				image13.setSize("110px", "110px");
				image13.setUrl("images/pickme.png");
				flexTable.setWidget(4,1,image14);
				image14.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						showPic(14);
					}
				});
				image14.setSize("110px", "110px");
				image14.setUrl("images/pickme.png");
				flexTable.setWidget(4,2,image15);
				image15.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						showPic(15);
					}
				});
				image15.setSize("110px", "110px");
				image15.setUrl("images/pickme.png");
				flexTable.setWidget(4,3,image16);
				image16.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						showPic(16);
					}
				});
				image16.setSize("110px", "110px");
				image16.setUrl("images/pickme.png");
				
				resetGame();
	}
	
	private void resetGame(){
		game.resetGame();
		showName();
	}
	
	private void showPic(int i){
		if(gameRunning && game.getData(i)!= PLAYERONE && game.getData(i) != PLAYERTWO && i!=game.getPick()){
		//show picture
		displayPic(i);
		//check for match
		if(game.getPick()==0){
			game.setPick(i);
			secondPick=false;
		}else{
			match = checkForMatch(i);
			secondPick=true;
		}
		if (match){
			game.incPlayer(game.getPlayer());
			game.setData(game.getPick(), game.getPlayer());
			game.setData(i, game.getPlayer());
			if(game.getPlayer()==9){
				lblPlayerName.setText("Croc has a match!!");
			} else{
				lblPlayerName.setText("Tiger has a match!!");
			}
			//change display			
		}
		if(checkForEndOfGame()){
			endOfGame();
		}else{
			if(secondPick){
				gameRunning = false;
				//btnreset.setVisible(true);
				t.schedule(DELAY);
				}
			}
		}
	}
	
	
	
	private boolean checkForEndOfGame(){
		if(game.getPlayer1() + game.getPlayer2()==8){
			return true;
		}
		return false;
	}
	
	private void endOfGame(){
		if(game.getPlayer1()> game.getPlayer2()){
			lblPlayerName.setText("Croc won!!");
			image.setUrl(game.getImage(PLAYERONE));
			
		}else if(game.getPlayer1()< game.getPlayer2()){
			lblPlayerName.setText("Tiger won!!");
			image.setUrl(game.getImage(PLAYERTWO));
		}else{
			lblPlayerName.setText("The game was drawn.");
			image.setUrl("images/pickme.png");
		}
		gameOver=true;
		btnreset.setText("Play Again?");
		//btnreset.setVisible(true);
		t.schedule(DELAY);
		
	}
	
	private void resetBoard(){
		if(game.getData(1)!=PLAYERONE && game.getData(1)!=PLAYERTWO){
			image1.setUrl("images/pickme.png");
			image1.setStyleName("notPicked");
		}else{
			image1.setUrl(getImage(game.cardPicked(1)));
			image1.setStyleName("pickedPic");
		}
		if(game.getData(2)!=PLAYERONE && game.getData(2)!=PLAYERTWO){
			image2.setUrl("images/pickme.png");
			image2.setStyleName("notPicked");
		}else{
			image2.setUrl(getImage(game.cardPicked(2)));
			image2.setStyleName("pickedPic");
		}
		if(game.getData(3)!=PLAYERONE && game.getData(3)!=PLAYERTWO){
			image3.setUrl("images/pickme.png");
			image3.setStyleName("notPicked");
		}else{
			image3.setUrl(getImage(game.cardPicked(3)));
			image3.setStyleName("pickedPic");
		}
		if(game.getData(4)!=PLAYERONE && game.getData(4)!=PLAYERTWO){
			image4.setUrl("images/pickme.png");
			image4.setStyleName("notPicked");
		}else{
			image4.setUrl(getImage(game.cardPicked(4)));
			image4.setStyleName("pickedPic");
		}
		if(game.getData(5)!=PLAYERONE && game.getData(5)!=PLAYERTWO){
			image5.setUrl("images/pickme.png");
			image5.setStyleName("notPicked");
		}else{
			image5.setUrl(getImage(game.cardPicked(5)));
			image5.setStyleName("pickedPic");
		}
		if(game.getData(6)!=PLAYERONE && game.getData(6)!=PLAYERTWO){
			image6.setUrl("images/pickme.png");
			image6.setStyleName("notPicked");
		}else{
			image6.setUrl(getImage(game.cardPicked(6)));
			image6.setStyleName("pickedPic");
		}
		if(game.getData(7)!=PLAYERONE && game.getData(7)!=PLAYERTWO){
			image7.setUrl("images/pickme.png");
			image7.setStyleName("notPicked");
		}else{
			image7.setUrl(getImage(game.cardPicked(7)));
			image7.setStyleName("pickedPic");
		}
		if(game.getData(8)!=PLAYERONE && game.getData(8)!=PLAYERTWO){
			image8.setUrl("images/pickme.png");
			image8.setStyleName("notPicked");
		}else{
			image8.setUrl(getImage(game.cardPicked(8)));
			image8.setStyleName("pickedPic");
		}
		if(game.getData(9)!=PLAYERONE && game.getData(9)!=PLAYERTWO){
			image9.setUrl("images/pickme.png");
			image9.setStyleName("notPicked");
		}else{
			image9.setUrl(getImage(game.cardPicked(9)));
			image9.setStyleName("pickedPic");
		}
		if(game.getData(10)!=PLAYERONE && game.getData(10)!=PLAYERTWO){
			image10.setUrl("images/pickme.png");
			image10.setStyleName("notPicked");
		}else{
			image10.setUrl(getImage(game.cardPicked(10)));
			image10.setStyleName("pickedPic");
		}
		if(game.getData(11)!=PLAYERONE && game.getData(11)!=PLAYERTWO){
			image11.setUrl("images/pickme.png");
			image11.setStyleName("notPicked");
		}else{
			image11.setUrl(getImage(game.cardPicked(11)));
			image11.setStyleName("pickedPic");
		}
		if(game.getData(12)!=PLAYERONE && game.getData(12)!=PLAYERTWO){
			image12.setUrl("images/pickme.png");
			image12.setStyleName("notPicked");
		}else{
			image12.setUrl(getImage(game.cardPicked(12)));
			image12.setStyleName("pickedPic");
		}
		if(game.getData(13)!=PLAYERONE && game.getData(13)!=PLAYERTWO){
			image13.setUrl("images/pickme.png");
			image13.setStyleName("notPicked");
		}else{
			image13.setUrl(getImage(game.cardPicked(13)));
			image13.setStyleName("pickedPic");
		}
		if(game.getData(14)!=PLAYERONE && game.getData(14)!=PLAYERTWO){
			image14.setUrl("images/pickme.png");
			image14.setStyleName("notPicked");
		}else{
			image14.setUrl(getImage(game.cardPicked(14)));
			image14.setStyleName("pickedPic");
		}
		if(game.getData(15)!=PLAYERONE && game.getData(15)!=PLAYERTWO){
			image15.setUrl("images/pickme.png");
			image15.setStyleName("notPicked");
		}else{
			image15.setUrl(getImage(game.cardPicked(15)));
			image15.setStyleName("pickedPic");
		}
		if(game.getData(16)!=PLAYERONE && game.getData(16)!=PLAYERTWO){
			image16.setUrl("images/pickme.png");
			image16.setStyleName("notPicked");
		}else{
			image16.setUrl(getImage(game.cardPicked(16)));
			image16.setStyleName("pickedPic");
		}	
	}
	
	private boolean checkForMatch(int i){
		if(game.getData(i) == game.getData(game.getPick())){
			return true;
		}else{
			return false;
		}
		
	}
	
	private void showName(){
		//display player name and number of card
		if(game.getPlayer()==9){
			lblPlayerName.setText("Croc: " + game.getPlayer1() + " cards matched.");
			image.setUrl(game.getImage(PLAYERONE));
		} else{
			lblPlayerName.setText("Tiger: " + game.getPlayer2() + " cards matched.");
			image.setUrl(game.getImage(PLAYERTWO));
		}
	}
	
	private String getImage(int i){
		String temp="images/pickme.png";
			if(game.getImage(i)!=null)
				temp = game.getImage(i);
		return temp;
	}
	
	private void displayPic(int i){
		switch (i){
			case 0: image.setUrl(getImage(game.cardPicked(i))); break;
			case 1: image1.setUrl(getImage(game.cardPicked(i))); break;
			case 2: image2.setUrl(getImage(game.cardPicked(i))); break;
			case 3: image3.setUrl(getImage(game.cardPicked(i))); break;
			case 4: image4.setUrl(getImage(game.cardPicked(i))); break;
			case 5: image5.setUrl(getImage(game.cardPicked(i))); break;
			case 6: image6.setUrl(getImage(game.cardPicked(i))); break;
			case 7: image7.setUrl(getImage(game.cardPicked(i))); break;
			case 8: image8.setUrl(getImage(game.cardPicked(i))); break;
			case 9: image9.setUrl(getImage(game.cardPicked(i))); break;
			case 10: image10.setUrl(getImage(game.cardPicked(i))); break;
			case 11: image11.setUrl(getImage(game.cardPicked(i))); break;
			case 12: image12.setUrl(getImage(game.cardPicked(i))); break;
			case 13: image13.setUrl(getImage(game.cardPicked(i))); break;
			case 14: image14.setUrl(getImage(game.cardPicked(i))); break;
			case 15: image15.setUrl(getImage(game.cardPicked(i))); break;
			case 16: image16.setUrl(getImage(game.cardPicked(i))); break;
		}
	}
	
}
